﻿using Sc.Domain;
using Sc.Persistence;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Service
{
    public class TrainService
    {
        private readonly TrainDAO dal = new TrainDAO();
		#region  BasicMethod
		/// <summary>
		/// 增加一条数据
		/// </summary>
        public int Add(Train model)
		{
            var id = dal.Add(model);
            if (id > 0)
            {
                new TrainDetailDAO().Add(model.TrainDetailList, id);
            }
            return id;
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
        public bool Update(Train model)
		{
            var id = dal.Update(model);
            if (id)
            {
                new TrainDetailService().Delete(model.Id);
                new TrainDetailDAO().Add(model.TrainDetailList, model.Id);
            }
            return id;
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			return dal.Delete(Id);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			return dal.DeleteList(Idlist );
		}

		#endregion  BasicMethod
        #region UserMethod
        /// <summary>
        /// 获取报废列表
        /// </summary>
        /// <param name="orgz">机构ID</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前第几页</param>
        /// <param name="start">用车开始时间</param>
        /// <param name="end">用车结束时间</param>
        /// <returns></returns>
        public CommModel<Train> GetList(int orgz, int pageSize, int pageIndex, string start, string end)
        {
            string where = string.Empty;
            if (orgz > 0)
            {
                where = "AND OrgzNo=" + orgz;
            }
            if (!string.IsNullOrEmpty(start))
            {
                where += " AND Time>='" + start + "' AND Time<='" + end + "'";
            }
            Hashtable param = new Hashtable();
            param.Add("A1", pageIndex);
            param.Add("A2", pageSize);
            param.Add("A3", where);
            param.Add("A4", "Vw_Trains");
            param.Add("A5", "Time DESC");
            var ht = CommDAO.CommPagination(param);
            CommModel<Train> ent = new CommModel<Train>();
            DataTable dt = ht["data"] as DataTable;
            List<Train> list = new List<Train>();
            foreach (DataRow item in dt.Rows)
            {
                list.Add(dal.DataRowToModel(item));
            }
            ent.Data = list;
            ent.PageCount = Convert.ToInt32(ht["PageCount"]);
            ent.RecordCount = Convert.ToInt32(ht["record"]);
            return ent;
        }

        #endregion
    }
}
